package com.cuit.zuke.system.controller;

import com.cuit.zuke.common.annotation.ControllerEndpoint;
import com.cuit.zuke.common.util.Result;
import com.cuit.zuke.system.entity.SysDepartmentEntity;
import com.cuit.zuke.system.service.SysDepartmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @program: zuke
 * @description: 部门
 * @author: KamTang
 * @create: 2022-04-11 10:18
 **/
@RequestMapping("/sys/department")
@RestController
public class SysDepartmentController {

    @Autowired
    private SysDepartmentService departmentService;

    /**
     * 展示部门
     * @param params
     * @return
     */
    @GetMapping("/list")
    public Result departmentList (@RequestParam(required = false) Map<String, Object> params) {
        return Result.ok().put("departmentList", departmentService.findDepartmentList(params));
    }

    /**
     * 删除部门
     * @param ids
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "部门删除失败", operation = "删除部门")
    @PostMapping("/delete")
    public Result deleteDept (@RequestBody Long[] ids) {
        departmentService.deleteBatch(ids);
        return Result.ok();
    }

    /**
     * 根据部门id获取部门信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id) {
        SysDepartmentEntity department = departmentService.getById(id);
        return Result.ok().put("department", department);
    }

    @PostMapping("/add")
    public Result addDept (@RequestBody SysDepartmentEntity department) {
        boolean flag = departmentService.save(department);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


    @PostMapping("/update")
    public Result updateDept (@RequestBody SysDepartmentEntity department) {
        boolean flag = departmentService.updateById(department);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
}
